package com.hks360.car_treasure.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.activity.AlertActivity;
import com.hks360.car_treasure.activity.ConnectEquipmentActivity;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.common.Method;
import com.hks360.car_treasure.common.PrefKey;
import com.hks360.car_treasure.internet.request_net.constant.NetActionName;
import com.hks360.car_treasure.internet.request_net.nohttp.user_interface.HttpCallback;
import com.hks360.car_treasure.internet.request_net.nohttp.user_javabean.twoState;
import com.hks360.car_treasure.internet.request_net.nohttp.user_method.CallServerManager;
import com.hks360.car_treasure.internet.request_net.nohttp.user_requstparam.UnBindUser;
import com.hks360.car_treasure.util.GsonUtil;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.PrefUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.nohttp.Response;
import com.hks360.third_party_library.util.GsonService;

import java.util.Date;

/**
 * Created by car_treasure on 2016/3/15.
 */
public class InfoDeviceFragment extends BaseFragment implements HttpCallback{
    private ListView mdeviceLv;
    private View view1;
    private  AlertDialog.Builder builder;
    private AlertDialog dialog;
    private EditText mPhoneDialog;
    private EditText mPasswordDialog;
    private TextView mRemoveDialog;
    private int tag = 0;
    private BaseAdapter MyAdapter;
    @Override
    public void setupView() {
        mdeviceLv = UIUtil.findViewById(mRootView, R.id.info_device_list);
        view1 = View.inflate(getContext(), R.layout.dialog_view_layout, null);
        mPhoneDialog = UIUtil.findViewById(view1, R.id.login_phone_dialog);
        mPasswordDialog = UIUtil.findViewById(view1, R.id.login_pass_dialog);
        mRemoveDialog = UIUtil.findViewById(view1,R.id.remove_bind_dialog);
        mRemoveDialog.setOnClickListener(this);
        builder = new AlertDialog.Builder(getContext());
        dialog = builder.create();
        dialog.setView(view1);

    }

    @Override
    public void initData() {
        MyAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                if (AppConfig.user.getTboxinfo().size() == 0) {
                    return 1;
                }
                return AppConfig.user.getTboxinfo().size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                if (AppConfig.user.getTboxinfo().size() != 0) {
                    view = View.inflate(getContext(), R.layout.userinfo_device_listitem, null);
                    Method.setI(i);
                    TextView mLicesenoTv;
                    final TextView mTboxIdTv;
                    TextView mTypeTv;
                    TextView mStartTimeTv;
                    TextView mSimCardTv;
                    TextView mDeviceNameTv;
                    TextView mRemoveBindTv;
                    RelativeLayout alertRl;
                    mLicesenoTv = UIUtil.findViewById(view, R.id.device_liceseno_tv);
                    mTboxIdTv = UIUtil.findViewById(view, R.id.device_location_tv);
                    mTypeTv = UIUtil.findViewById(view, R.id.device_type_tv);
                    mStartTimeTv = UIUtil.findViewById(view, R.id.device_starttime_tv);
                    mSimCardTv = UIUtil.findViewById(view, R.id.device_simcard_tv);
                    mDeviceNameTv = UIUtil.findViewById(view, R.id.tv_devicename);
                    mRemoveBindTv = UIUtil.findViewById(view, R.id.remove_bind_tv);
                    alertRl = UIUtil.findViewById(view,R.id.alert_tv);
                    alertRl.setTag(i);
                    mRemoveBindTv.setTag(i);
                    mRemoveBindTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            tag = (int) view.getTag();
                            dialog.show();
                        }
                    });
                    if (Method.getCph().isEmpty()) {
                        mLicesenoTv.setText("车牌号:暂无");
                    } else {
                        mLicesenoTv.setText(getString(R.string.caracount, Method.getCph()));
                    }
                    alertRl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            tag = (int) view.getTag();
                            CommonUtil.showToast(getContext(), "打开车辆警告界面");
                            Intent intent = new Intent();
                            intent.setClass(getContext(),AlertActivity.class);
                            intent.setData(Uri.parse(Method.getTboxId(tag)));
                            startActivity(intent);
                        }
                    });
                    mTboxIdTv.setText(getString(R.string.locate, Method.getTboxId()));
                    mDeviceNameTv.setText(getString(R.string.devicename, Method.getDeviceName()));
                    mTypeTv.setText("");
                    if (Method.getStartTime() == "") {
                        mStartTimeTv.setText("");
                    } else {
                        String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date(Long.parseLong(Method.getStartTime()) * 1000));
                        mStartTimeTv.setText(date);
                    }
                    mSimCardTv.setText(Method.getSimCard());
                } else {
                    view = View.inflate(getContext(), R.layout.userinfo_device_listitem_null, null);
                    TextView link;
                    link = UIUtil.findViewById(view,R.id.link_device);
                    link.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CommonUtil.openActivity(getContext(), ConnectEquipmentActivity.class);
                        }
                    });

                }
                return view;
            }
        };
    }

    @Override
    public void addListener() {
        mdeviceLv.setAdapter(MyAdapter);
    }

    @Override
    public void initView() {
        setupView();
        initData();
        addListener();
    }

    @Override
    public void onDestroyView() {
        Method.setI(PrefUtil.getInt(getContext(),PrefKey.COUNT,0));
        super.onDestroyView();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.infodevice_layout,null);
        initView();
        return mRootView;
    }

    @Override
    public void viewClick(View v) {
        switch (v.getId()){
            case R.id.remove_bind_dialog:
                if(mPhoneDialog.getText().toString().trim().equals(PrefUtil.getString(getContext(),PrefKey.LOGIN_PHONE,""))  &&
                        mPasswordDialog.getText().toString().trim().equals(PrefUtil.getString(getContext(),PrefKey.LOGIN_PASSWORD,""))){
                    CallServerManager.PostServer(R.id.remove_bind_dialog, GsonUtil.toJson(new UnBindUser(mPhoneDialog.getText().toString().trim(),
                                    mPasswordDialog.getText().toString().trim(), Method.getTboxId(tag))),
                            NetActionName.UNBINDUSER, this);
                    dialog.dismiss();
                }else {
                    CommonUtil.showToast(getContext(),"输出信息错误");
                    mPhoneDialog.setText("");
                    mPasswordDialog.setText("");
                }

                break;
        }


    }

    @Override
    public void onSuccess(int what, Response response) {
        if(response.toString().contains("ok")){
            CommonUtil.showToast(getContext(),"解绑成功");
            AppConfig.user.getTboxinfo().remove(tag);
            MyAdapter.notifyDataSetChanged();
        }else {
            CommonUtil.showToast(getContext(),"解绑出错!");
        }
    }

    @Override
    public void onFailed(int what) {

    }

    @Override
    public void onStart(int what) {

    }

    @Override
    public void onFinish(int what) {

    }
}
